/**
   \file  singleton.hh
   \brief Defines a generic singleton class.
*/

/*
   This file is part of libgist.

   libgist is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   libgist is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with libgist; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

/*
   REVISION HISTORY

   $HeadURL: http://libgist.googlecode.com/svn/branches/wu_dev/drv/singleton.hh $
   $Id: singleton.hh 41 2011-08-22 08:51:48Z libgist@gmail.com $
*/

#ifndef GISTDRV_SINGLETON_DOT_HH
#define GISTDRV_SINGLETON_DOT_HH

//----------------------- NAMESPACE DEFINITION --------------------------

namespace gistdrv {

//------------------------- CLASS DEFINITION ----------------------------

/**
   \brief A generic singleton class.

   This template allows separation of the concept of a singleton from the
   actual implementation of the concept. The way to use this class is as
   follows:

   <ol>
     <li>Derive the "client" class (say X) from this one.</li>
     <li>Make singleton<X> a friend of X.</li>
     <li>
       Make X's constructor private to prevent others from instantiating
       it.
     </li>
   </ol>
*/
template<typename T>
class singleton {
   // Prevent copy and assignment
   singleton(const singleton&) ;
   singleton& operator=(const singleton&) ;

protected:
   // Initialization and clean-up
   singleton(){}
   virtual ~singleton(){}

public:
   /**
      \brief  Return reference to lone instance of the singleton object.
      \return Reference to object of type T that is the lone instance of
      the singleton class T.

      This function returns a reference to the lone instance of the
      singleton class T. The first invocation of this function will
      create the object. Unfortunately, as this is a somewhat simplistic
      implementation of the singleton concept, the client class T can
      only be default constructed.
   */
   static T& instance() {static T t ; return t ;}
} ;

//-----------------------------------------------------------------------

} // end of namespace encapsulating this file's definitions

#endif

/* So things look consistent in everyone's emacs... */
/* Local Variables: */
/* indent-tabs-mode: nil */
/* End: */
